package com.ruoyi.yljf.service.impl;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.yljf.domain.Active;
import com.ruoyi.yljf.domain.Article;
import com.ruoyi.yljf.domain.Course;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.domain.vo.*;
import com.ruoyi.yljf.mapper.ActiveMapper;
import com.ruoyi.yljf.mapper.ArticleMapper;
import com.ruoyi.yljf.mapper.CourseMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.PharmacyMapper;
import com.ruoyi.yljf.domain.Pharmacy;
import com.ruoyi.yljf.service.IPharmacyService;
import javax.annotation.Resource;

/**
 * 门诊医堂Service业务层处理
 * 
 * @author TTTc
 * @date 2025-02-27
 */
@Service
public class PharmacyServiceImpl extends ServiceImpl<PharmacyMapper, Pharmacy> implements IPharmacyService {

    @Resource
    private PharmacyMapper pharmacyMapper;
	@Resource
	private ActiveMapper activeMapper;
	@Resource
	private ArticleMapper articleMapper;
	@Resource
	private CourseMapper courseMapper;
    
    /**
     * 查询门诊医堂列表
     * 
     * @param pharmacy 门诊医堂
     * @return 门诊医堂
     */
    @Override
    public List<Pharmacy> selectPharmacyList(Pharmacy pharmacy) {
    	LambdaQueryWrapper<Pharmacy> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(pharmacy.getPharmacyId())) {
			queryWrapper.eq(Pharmacy::getPharmacyId, pharmacy.getPharmacyId());
		}
        if (StringUtils.isNotEmpty(pharmacy.getPharmacyImg())) {
			queryWrapper.eq(Pharmacy::getPharmacyImg, pharmacy.getPharmacyImg());
		}
        if (StringUtils.isNotEmpty(pharmacy.getPharmacyVideo())) {
			queryWrapper.eq(Pharmacy::getPharmacyVideo, pharmacy.getPharmacyVideo());
		}
        if (StringUtils.isNotEmpty(pharmacy.getPharmacyName())) {
			queryWrapper.like(Pharmacy::getPharmacyName, pharmacy.getPharmacyName());
		}
        if (StringUtils.isNotEmpty(pharmacy.getPharmacyAddress())) {
			queryWrapper.eq(Pharmacy::getPharmacyAddress, pharmacy.getPharmacyAddress());
		}
        if (StringUtils.isNotEmpty(pharmacy.getContactPersion())) {
			queryWrapper.eq(Pharmacy::getContactPersion, pharmacy.getContactPersion());
		}
        if (StringUtils.isNotEmpty(pharmacy.getContactPhone())) {
			queryWrapper.eq(Pharmacy::getContactPhone, pharmacy.getContactPhone());
		}
        if (StringUtils.isNotEmpty(pharmacy.getPharmacyNumber())) {
			queryWrapper.eq(Pharmacy::getPharmacyNumber, pharmacy.getPharmacyNumber());
		}
        if (StringUtils.isNotEmpty(pharmacy.getSpeciality())) {
			queryWrapper.eq(Pharmacy::getSpeciality, pharmacy.getSpeciality());
		}
        return pharmacyMapper.selectList(queryWrapper);
    }

	@Override
	public List<HomePharmacyVO> getPharmacy() {

		List<Pharmacy> pharmacies = pharmacyMapper.selectList(
				new LambdaQueryWrapper<>(Pharmacy.class)
						.last("LIMIT 4")
		);
		return pharmacies.stream().map(HomePharmacyVO::new).collect(Collectors.toList());
	}

	@Override
	public PharmacyInfoVO queryById(Long pharmacyId) {
		Pharmacy pharmacy = pharmacyMapper.selectById(pharmacyId);
		PharmacyInfoVO pharmacyInfoVO = new PharmacyInfoVO(pharmacy);


		ArrayList<String> resList = new ArrayList<>();
		// 1. 使用 split(",") 分割字符串
		if (StringUtils.isNotEmpty(pharmacy.getPharmacyVideo())) {
			String[] videoArray = pharmacy.getPharmacyVideo().split(",");
			List<String> videoList = Arrays.asList(videoArray);
			resList.addAll(videoList);
		}
		if (StringUtils.isNotEmpty(pharmacy.getPharmacyImg())) {
			String[] imgArray = pharmacy.getPharmacyImg().split(",");
			List<String> imgList = Arrays.asList(imgArray);
			resList.addAll(imgList);

		}
		pharmacyInfoVO.setBanner(resList);
		List<Course> courses = courseMapper.selectList(
				new LambdaQueryWrapper<>(Course.class)
						.eq(Course::getCreateType, yljfEnums.getCodeByEnum(yljfEnums.PHARMACY))
						.eq(Course::getDelFlag, 0)
						.eq(Course::getCreateId, pharmacyId)
						.orderByDesc(Course::getId)
		);
		if (ObjectUtils.isNotNull(courses)) {
			List<HomeCourseVO> collect = courses.stream().map(HomeCourseVO::new).collect(Collectors.toList());
			pharmacyInfoVO.setCourseVOList(collect);
		}

		List<Article> articles = articleMapper.selectList(
				new LambdaQueryWrapper<>(Article.class)
						.eq(Article::getCreateType, yljfEnums.getCodeByEnum(yljfEnums.PHARMACY))
						.eq(Article::getDelFlag, 0)
						.eq(Article::getCreateId, pharmacyId)
						.orderByDesc(Article::getId)
		);
		if (ObjectUtils.isNotNull(articles)) {
			List<ActiveArticleVO> collect = articles.stream().map(ActiveArticleVO::new).collect(Collectors.toList());
			pharmacyInfoVO.setArticleVOList(collect);
		}

		return pharmacyInfoVO;
	}

	@Override
	public List<?> getSpaceInfo(String type) {
		List<?> collect = null;
		if ("active".equals(type)){	//查询活动
			List<Active> actives = activeMapper.selectList(
					new LambdaQueryWrapper<>(Active.class)
							.orderByDesc(Active::getActiveId)
							.eq(Active::getDelFlag,0)

							.last("LIMIT 4")
			);
			collect = actives.stream().map(HomeActiveVO::new).collect(Collectors.toList());
		}else if ("pharmacy".equals(type)){ //医馆
			List<Pharmacy> pharmacies = pharmacyMapper.selectList(
					new LambdaQueryWrapper<>(Pharmacy.class)
							.orderByDesc(Pharmacy::getPharmacyId)
							.eq(Pharmacy::getDelFlag,0)
							.last("LIMIT 4")
			);
			collect = pharmacies.stream().map(HomePharmacyVO::new).collect(Collectors.toList());
		} else if ("article".equals(type)) {
			List<Article> articles = articleMapper.selectList(
					new LambdaQueryWrapper<>(Article.class)
							.eq(Article::getCreateType, yljfEnums.getCodeByEnum(yljfEnums.USER))
							.orderByDesc(Article::getId)
							.eq(Article::getCherryPick,"Y")
							.eq(Article::getDelFlag,0)
							.last("LIMIT 4")
			);
			collect = articles.stream().map(ActiveArticleVO::new).collect(Collectors.toList());

		}
		return collect;
	}

	@Override
	public List<Active> getActive() {
		return activeMapper.selectList(
				new LambdaQueryWrapper<>(Active.class)
						.orderByDesc(Active::getActiveId)
						.eq(Active::getDelFlag,0)
		);
	}

	@Override
	public List<Pharmacy> getPharmacyV2() {
		return pharmacyMapper.selectList(
				new LambdaQueryWrapper<>(Pharmacy.class)
						.orderByDesc(Pharmacy::getPharmacyId)
						.eq(Pharmacy::getDelFlag,0)
		);
	}

	@Override
	public List<Article> getArticle() {
		return articleMapper.selectList(
				new LambdaQueryWrapper<>(Article.class)
						.eq(Article::getCreateType, yljfEnums.getCodeByEnum(yljfEnums.USER))
						.orderByDesc(Article::getId)
						.eq(Article::getCherryPick,"Y")
						.eq(Article::getDelFlag,0));
	}
}
